# coding:utf-8
import numpy as np
from qregpy import qreg
import pandas as pd
from scipy import integrate
import pickle
from datetime import datetime

print("first we load data and sort it")
data = pd.read_table("../orders.tbl", sep='|')

data.sort_values('o_totalprice', inplace=True)
data.reset_index(drop=True, inplace=True)

print("finish data preprocess")

with open('../reg.pkl', 'rb') as f:
    reg = pickle.load(f)


def f_p(*args):
    return reg.predict([[args[0]]])[0]


count = 0
result_list = []
time_list = []

start = datetime.now()

total_min = 0
total_max = len(data) - 1

row_min = 0
row_max = 0

ans = float(0)

data = data.values

for row in data:
    pre_row_min = row_min
    pre_row_max = row_max
    row_min = count - 10000
    if row_min < total_min:
        row_min = total_min
    row_max = count + 10000
    if row_max > total_max:
        row_max = total_max

    if count % 10000 == 0:
        ans = integrate.quad(f_p, row_min, row_max, epsabs=10.0, epsrel=0.1)[0] / (row_max - row_min + 1)
    else:
        x_min = pre_row_min
        x_max = pre_row_max

    result_list.append(ans)
    count = count + 1

end = datetime.now()
time_cost = (end - start).total_seconds()
print("time cost {}".format(time_cost))

result_list = np.array(result_list)

print("now we calculate avg relative error")
standard_result = pd.read_csv("row_avg1g.csv")['avg']
standard_result = standard_result.values
relative_error = (abs(standard_result - result_list) / standard_result).mean() * 100

print("relative error is: {}%".format(relative_error))
